//
//  MoveMouseCommand.m
//  ScriptableDemo
//
//  Created by lg on 2020/5/26.
//  Copyright © 2020 lg. All rights reserved.
//

#import "LGMoveMouseCommand.h"

@implementation LGMoveMouseCommand

- (id)performDefaultImplementation {
    NSDictionary *args = [self evaluatedArguments];
    NSArray *points = [args objectForKey:@"point"];
    NSLog(@"test %@", points);
    
    if (points.count < 2) {
        return @(NO);
    }
    CGPoint point = CGPointMake([points[0] floatValue], [points[1] floatValue]);
    [self moveMouseToPoint:point];
    return @(YES);
}

// 移动鼠标到指定位置
- (void)moveMouseToPoint:(CGPoint)point {
    // 开启辅助功能权限才有效果
    CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStatePrivate);
    CGEventRef theEvent = CGEventCreateMouseEvent (source, kCGEventMouseMoved, point, 0);
    //    CGEventSetIntegerValueField(theEvent, kCGMouseEventClickState, 1);
    //    CGEventSetType(theEvent, kCGEventMouseMoved);
    CGEventPost(kCGHIDEventTap, theEvent);
    CFRelease(theEvent);
    CFRelease(source);
}
@end
